
import wizard
import pooler
from tools.misc import UpdateableStr,UpdateableDict
import time
import operator
from osv import osv, fields
from osv.orm import intersect
import tools.sql
from tools import config
from tools.translate import _
from mx import DateTime

class wizard_orders_detail_sheet(wizard.interface):
    def _get_defaults(self, cr, uid, data, context={}):
        pool = pooler.get_pool(cr.dbname)
        prj_obj = pool.get('account.analytic.account')
        ids = []
        check=False
        try:
            prj_ids=data['ids']
            check=True
        except:
            prj_ids=data['id']
        if prj_ids:
            list_prjs=prj_obj.browse(cr,uid,prj_ids)
            for x in list_prjs:
                for ord in x.order_ids:
                    ids.append(ord.id)
        if check:
            data['ids']=ids
        else:
            data['id']=ids
        #ids=data['ids']
        return data['form']
    
    def _get_records(self, cr, uid, data, context={}):
        pool = pooler.get_pool(cr.dbname)
        prj_obj = pool.get('account.analytic.account')
        ids = []
        check=False
        try:
            prj_ids=data['ids']
            check=True
        except:
            prj_ids=data['id']
        if prj_ids:
            list_prjs=prj_obj.browse(cr,uid,prj_ids)
            for x in list_prjs:
                for ord in x.order_ids:
                    ids.append(ord.id)
        data['model']='purchase.order'
        if check:
            data['ids']=ids
        else:
            data['id']=ids
        #ids=data['ids']
        data['active_ids']=ids
        if ids:
            data['active_id']=ids[0]
            id=ids[0]            
        return {'ids' : ids ,'model':'purchase.order', 'title' : 'Orders Detail','_terp_model':'purchase.order',} 
        
    states = {
        'init': {
            'actions': [_get_defaults],
            'result': {
                'type': 'state',
                'state':'print_report'
            },
        },
        'print_report': {
            'actions': [_get_records],
            'result': {
                'type': 'print',
                'name' : 'Order Details',
                'report': 'orders.detail',
                'rml' : 'account_analytic_summary/report/project_orders_detail.jrxml',
                'state': 'end'
                        },
                },
}
wizard_orders_detail_sheet('jreports.orders_detail_sheet.wizard')



import wizard
import pooler
import datetime
from tools.translate import _
from mx import DateTime


class wizard_liste_factures_clients_produits(wizard.interface):

    def _get_defaults(self, cr, uid, data, context={}):
        now=datetime.datetime.now()
        now= datetime.date(now.year, now.month, 1) - datetime.timedelta(days=1) # previous month
        data['form']['period'] = 'm'
        data['form']['month'] = str(now.month)
        data['form']['year'] = str(now.year)
        data['form']['draft'] = False
        return data['form']
        
    def _get_records(self, cr, uid, data, context={}):
        inv_obj = pooler.get_pool(cr.dbname).get('account.invoice')
        ids = []
        if data['form']['draft']==True:
            states = ['draft','open','paid']
        else:
            states = ['open','paid']
        
        title = _("Revenue per customer per product - ")
        if data['form']['period'] == 'm':
            if int(data['form']['month']) < 10:
                title += "0"
            title += data['form']['month'] + "/" + data['form']['year']
            day_min = datetime.date(int(data['form']['year']), int(data['form']['month']), 1)
            nextmonth = int(data['form']['month']) + 1
            year = int(data['form']['year'])
            if nextmonth == 13:
                nextmonth = 1
                year += 1
            day_max = datetime.date(year, nextmonth, 1) - datetime.timedelta(days=1)
        elif data['form']['period'] == 's':
            day_min = DateTime.strptime(data['form']['date_from'], '%Y-%m-%d')        
            day_max = DateTime.strptime(data['form']['date_to'], '%Y-%m-%d')        
            title += _("From ") + day_min.strftime('%d/%m/%Y') + _(" to ") + day_max.strftime('%d/%m/%Y') 
        else:
            day_min = datetime.date(int(data['form']['year']), 1, 1)
            day_max = datetime.date(int(data['form']['year']), 12, 31)
            title += data['form']['year']
        inv_ids = inv_obj.search(cr,uid,[
                  ('date_invoice','>=',day_min.strftime('%Y-%m-%d')),
                  ('date_invoice','<=',day_max.strftime('%Y-%m-%d')),
                  ('type','in',['out_invoice','out_refund']),
                  ('state','in',states),
               ])

        ids = []
        if inv_ids:
            lines=inv_obj.browse(cr,uid,inv_ids)
            for x in lines:
                for inv_line in x.invoice_line:
                    if inv_line.price_subtotal > 0.0:
                        ids.append(inv_line.id)

        return {'ids' : ids , 'title' : title, 'period' : data['form']['period'], 'year':data['form']['year']} 
        
    states = {
        'init': {
            'actions': [_get_defaults],
            'result': {
                'type': 'form',
                'arch':liste_factures_clients_produits_form,
                'fields':liste_factures_clients_produits_fields,
                'state':[('end','Cancel'),('print_report','Print')]
            },
        },
        'print_report': {
            'actions': [_get_records],
            'result': {
                'type': 'print',
                'name' : 'Revenue per customer per products',
                'report': 'ns_sales_reports.liste_factures_clients_produits',
                'rml' : 'ns_sales_reports/report/liste_factures_clients_produits.jrxml',
                'state': 'end'
            },
        },
    }

wizard_liste_factures_clients_produits('jreports.ns_sales_clients_products.wizard')

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
